package com.bsg.upm.controller;

import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;

@Controller
@RequestMapping("/v1.0/logs")
public class LogController {

	@RequestMapping(value = "/{level}", method = RequestMethod.PUT)
	public RespJson changeLogDevel(@PathVariable("level") String logLevel, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			Level level = Level.INFO;
			if (logLevel.toUpperCase().equals("OFF")) {
				level = Level.OFF;
			} else if (logLevel.toUpperCase().equals("FATAL")) {
				level = Level.FATAL;
			} else if (logLevel.toUpperCase().equals("ERROR")) {
				level = Level.ERROR;
			} else if (logLevel.toUpperCase().equals("WARN")) {
				level = Level.WARN;
			} else if (logLevel.toUpperCase().equals("INFO")) {
				level = Level.INFO;
			} else if (logLevel.toUpperCase().equals("DEBUG")) {
				level = Level.DEBUG;
			} else if (logLevel.toUpperCase().equals("TRACE")) {
				level = Level.TRACE;
			} else if (logLevel.toUpperCase().equals("ALL")) {
				level = Level.ALL;
			}
			LogManager.getRootLogger().setLevel(level);
		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
}
